﻿using System;

class MostFrequentNumber
{
    static void Main()
    {
        //Exercise 9.
        //Write a program that finds the most frequent number in an array. Example:
        //{4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} -> 4 (5 times)

        //enter the length and the elements of the array from the console
        Console.Write("Type the length of the array (N):  ");
        int N = int.Parse(Console.ReadLine());
        Console.WriteLine("Type the elements of the array:");
        int[] array = new int[N];
        for (int i = 0; i < N; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }

        Array.Sort(array);

        //int[] array = { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 };
        //Array.Sort(array);

        int sequenceLength = 1;
        int bestNumber = 0;
        int bestSequenceLength = 0;

        for (int i = 0; i < array.Length - 1; i++)
        {
            if (array[i] == array[i + 1])
            {
                sequenceLength++;           //if the two elements are equal, the counter in increased

                if (sequenceLength > bestSequenceLength)
                {
                    bestSequenceLength = sequenceLength;
                    bestNumber = array[i];
                }
            }
            else
            {
                sequenceLength = 1;          //else the counter is set at its starting point          
            }
        }
        Console.WriteLine();

        Console.Write("The most frequent number is: {0} ({1}) times", bestNumber, bestSequenceLength);
        
        Console.WriteLine();

    }
}

